package com.leehongrim.platformai.modules.controller;

//import com.hw.langchain.llms.ollama.Ollama;
//import com.leehongrim.platformai.common.util.OllamaUtils;
import com.alibaba.fastjson2.JSON;
import com.leehongrim.platformai.request.AiReq;
import com.leehongrim.platformai.request.AiRes;
import com.leehongrim.platformai.request.ResBody;
import com.leehongrim.platformai.request.Result;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Slf4j
@RequestMapping("/ai")
public class OllamaController {

    /**
     * chat
//     * @param question
     * @return
     */
//    @GetMapping("/chat")
//    public String chat(@RequestParam("question") String question){
//        log.info(" String question is {}",  question);
//        String answer = OllamaUtils.send(question);
//        log.info(" String answer is {}",  answer);
//        return answer;
//    }

    @PostMapping("/chat/send")
    @ResponseBody
    public Result chatSend(@RequestBody AiReq aiReq) {
        Result result = new Result();
        result.setCode(200);

        log.warn("aiReq={}", JSON.toJSONString(aiReq));
        List<Map<String, Object>> promptList = new ArrayList();
        if(aiReq.getContent() != null  && !aiReq.getContent().isEmpty()) {
            Map<String, Object> typeMap = new HashMap<>();
            typeMap.put("type", "text");
            typeMap.put("text", aiReq.getContent());
            promptList.add(typeMap);
        }
        if(aiReq.getImages() != null  && !aiReq.getImages().isEmpty()) {
            Map<String, Object> typeMap = new HashMap<>();
            typeMap.put("type", "image_url");
            typeMap.put("image_url", aiReq.getImages().get(0).get("url"));
            promptList.add(typeMap);
        }

        Map<String, Object> body = new HashMap<>();
        body.put("prompt", promptList);
        body.put("max_tokens", 2147483647);
        body.put("do_sample", false);
        body.put("stream", aiReq.isStream());
        body.put("model", "qwen2_vl");

        log.warn("body={}", JSON.toJSONString(body));
        HttpResponse<String> response = null;
        try{
            response = Unirest.post("http://localhost:8085/generate")
                    .body(body)
                    .asString();
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.setCode(500);
            result.setMessage(e.getMessage());
        }

        ResBody resBody = new ResBody();
        if (response != null) {
            resBody.setContent(response.getBody());
        }
        resBody.setTimestamp(System.currentTimeMillis());

        AiRes aiRes = new AiRes();
        aiRes.setConversationId("1");
        aiRes.setResponse(resBody);


        result.setData(aiRes);
        if (response != null) {
            result.setCode(response.getStatus());
        }

        result.setAiReq(aiReq);

        return result;
    }

}
